<!-- 首页 -->
<template>
	<view class="page">
		<u-tabs-swiper ref="uTabs" :list="tabs" current="current" @change="tabsChange"></u-tabs-swiper>
		<swiper :current="swiperCurrent" @transition="transition" @animationfinish="animationfinish">
			<swiper-item class="swiper-item" v-for="(item, index) in tabs" :key="index">
				<scroll-view scroll-y style="height: 800rpx;width: 100%;" @scrolltolower="onreachBottom">
					...
				</scroll-view>
			</swiper-item>
		</swiper>
		<talk-list @more="more" @chat="chat" :dataList="dataList"></talk-list>
		<view class="loadmore-box" v-if="dataList.length > 0">
			<u-loadmore :status="status" @loadmore="load"></u-loadmore>
		</view>
		<u-back-top :scrollTop="scrollTop"></u-back-top>
		<user-auth></user-auth>
	</view>
</template>

<script>
export default {
	data() {
		return {
			tabs: [{
				name: '关注'
			}, {
				name: '推荐'
			}, {
				name: '最新'
			}],
			current: 0, // tabs组件的current值，表示当前活动的tab选项
			swiperCurrent: 0, // swiper组件的current值，表示当前那个swiper-item是活动的
			form: {
				lon: '',
				lat: '',
			},
			status: 'loadmore',
			scrollTop: 0,
			isEnd: false,
			current: 1,
			pageSize: 10,
			dataList: []
		};
	},
	watch: {},
	mounted() {
		this.query();
	},
	methods: {
		tabsChange(index) {
			this.swiperCurrent = index;
		},
		transition(e) {
			let dx = e.detail.dx;
			this.$refs.uTabs.setDx(dx);
		},
		animationfinish(e) {
			let current = e.detail.current;
			this.$refs.uTabs.setFinishCurrent(current);
			this.swiperCurrent = current;
			this.current = current;
		},
		// scroll-view到底部加载更多
		onreachBottom() {
			
		},
		reload() {
			this.query();
		},
		load() {
			this.loadmore();
		},
		onScrollTop(e) {
			this.scrollTop = e.scrollTop;
		},
		async loadmore() {
			this.status = 'loading';
			if(this.isEnd){
				this.status = 'nomore';
				return;
			}
			var res = await this.getDataList();
			this.dataList = this.dataList.concat(res.data.records);
		},
		updatePosition() {
			this.$u.put('/system/user/profile',{
				lon: this.form.lon,
				lat: this.form.lat,
			}).then(res => {
				
			})
		},
		query(tip) {
			this.current = 1;
			this.isEnd = false;
			this.status = 'loadmore';
			if('0' == tip){
				
			}
			this.loadquery();
			this.$emit('fresh');
		},
		async loadquery() {
			var res = await this.getDataList();
			this.dataList = res.rows;
		},
		async getDataList() {
			var res = await this.$u.get('/wq/dynamic/list', {
				pageNum: this.current,
				pageSize: this.pageSize,
			});
			var totalPage = Math.ceil(res.total/this.pageSize);
			this.pages = totalPage;
			this.current ++;
			this.status = 'loadmore';
			if(this.current > this.pages){
				this.status = 'nomore';
				this.isEnd = true;
			}
			return res;
		},
		chat(e) {
			this.$u.route({
				url: 'packgeMine/pages/index/dynamic',
				params: {
					dynamicId: e.id,
				}
			});
		},
		more() {
			this.$u.toast('打开');
		}
	}
};
</script>
<style lang="scss" scoped>
	page {
		background-color: #fff;
	}
	
	.loadmore-box {
		padding: 10px 0;
	}
</style>
